package com.tencent.matrix.trace.c;

import android.os.Build;
import android.os.Handler.Callback;
import android.os.Message;
import android.os.SystemClock;

import androidx.annotation.NonNull;
import androidx.customview.widget.ExploreByTouchHelper;

import com.tencent.matrix.g.Matrix_G_C;
import com.tencent.matrix.trace.core.AppMethodBeat;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class Trace_C_A {
    public static int cEA = ExploreByTouchHelper.INVALID_ID;
    private static long cEv = 0;
    private static long cEw = 0;
    private static long cEx = 0;
    public static AppMethodBeat.a cEz = new AppMethodBeat.a();
    public static AppMethodBeat.a cEy = new AppMethodBeat.a();

    static final class a implements Callback {
        private static boolean cEB = false;
        private static int cEC = 10;
        private final Callback cED;
        private Method method = null;

        a(Callback callback) {
            this.cED = callback;
        }

        public final boolean handleMessage(@NonNull Message message) {
            if (AppMethodBeat.isRealTrace()) {
                boolean g = g(message);
                if (cEC > 0) {
                    Matrix_G_C.i("Matrix.ActivityThreadHacker", "[handleMessage] msg.what:%s begin:%s isLaunchActivity:%s", message.what, SystemClock.uptimeMillis(), g);
                    cEC--;
                }
                if (g) {
                    Trace_C_A.cEx = SystemClock.uptimeMillis();
                    Trace_C_A.cEy = AppMethodBeat.getInstance().maskIndex("LastLaunchActivityMethodIndex");
                }
                if (!cEB && (g || message.what == 114 || message.what == 113)) {
                    Trace_C_A.cEw = SystemClock.uptimeMillis();
                    Trace_C_A.cEA = message.what;
                    cEB = true;
                }
                return this.cED != null && this.cED.handleMessage(message);
            } else {
                return this.cED != null && this.cED.handleMessage(message);
            }
        }

        private boolean g(Message message) {
            if (Build.VERSION.SDK_INT <= 27) {
                return message.what == 100;
            } else {
                if (message.what == 159 /*JsApiGetBackgroundAudioState.CTRL_INDEX*/ && message.obj != null) {
                    try {
                        if (this.method == null) {
                            this.method = Class.forName("android.app.servertransaction.ClientTransaction").getDeclaredMethod("getCallbacks");
                            this.method.setAccessible(true);
                        }
                        List list = (List) this.method.invoke(message.obj, new Object[0]);
                        if (!list.isEmpty()) {
                            return list.get(0).getClass().getName().endsWith(".LaunchActivityItem");
                        }
                    } catch (Exception e) {
                        Matrix_G_C.e("Matrix.ActivityThreadHacker", "[isLaunchActivity] %s", e);
                    }
                }
                return message.what == 100;
            }
        }
    }

    public static void Ie() {
        try {
            cEv = SystemClock.uptimeMillis();
            cEz = AppMethodBeat.getInstance().maskIndex("ApplicationCreateBeginMethodIndex");
            Class cls = Class.forName("android.app.ActivityThread");
            Field declaredField = cls.getDeclaredField("sCurrentActivityThread");
            declaredField.setAccessible(true);
            Object obj = declaredField.get(cls);
            Field declaredField2 = cls.getDeclaredField("mH");
            declaredField2.setAccessible(true);
            obj = declaredField2.get(obj);
            Field declaredField3 = obj.getClass().getSuperclass().getDeclaredField("mCallback");
            declaredField3.setAccessible(true);
            declaredField3.set(obj, new a((Callback) declaredField3.get(obj)));
            Matrix_G_C.i("Matrix.ActivityThreadHacker", "hook system handler completed. start:%s SDK_INT:%s", cEv, Build.VERSION.SDK_INT);
        } catch (Exception e) {
            Matrix_G_C.e("Matrix.ActivityThreadHacker", "hook system handler err! %s", e.getCause());
        }
    }
}
